﻿using System;
using System.Reflection;

namespace ReflectionExample
{
    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public void Introduce()
        {
            Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 获取 Person 类型
            Type personType = typeof(Person);

            // 获取构造方法信息
            ConstructorInfo constructor = personType.GetConstructor(new Type[] { typeof(string), typeof(int) });

            if (constructor != null)
            {
                // 构造参数
                object[] parameters = { "Alice", 30 };

                // 使用反射调用构造方法创建实例对象
                Person person = (Person)constructor.Invoke(parameters);

                // 调用对象方法
                person.Introduce();
            }
            else
            {
                Console.WriteLine("Constructor not found.");
            }
            Console.ReadLine();
        }
    }
}

